package cn.tedu.bonfire.upload.controller;


import cn.tedu.bonfire.common.enumerator.ServiceCode;
import cn.tedu.bonfire.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.bonfire.common.web.JsonResult;
import cn.tedu.bonfire.upload.qiniuyunutil.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 上传
 */
@Slf4j
@RestController
@RequestMapping("upload")
@Api(tags = "1.上传文件模块")
public class UploadController {


//    @ApiOperation("上传视频")
//    @PostMapping("uploadVideo")
//    public JsonResult uploadVideo(@RequestParam("file") MultipartFile file,
//                                   CurrentPrincipal currentPrincipal) throws IOException {
//        String suffix = file.getOriginalFilename().substring(
//                file.getOriginalFilename().lastIndexOf(".")
//        );
//
//        if (!suffix.equalsIgnoreCase(".mp4")){
//            return JsonResult.fail(ServiceCode.ERROR_UPLOAD_EMPTY,"上传文件格式错误");
//        }
//        final String newFileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"))+currentPrincipal.getId();
//        final String uploadUrl = FileUploadUtil.uploadVideo(file.getBytes(), newFileName + suffix);
////        System.out.println(uploadUrl);
//        return JsonResult.ok(uploadUrl);
//    }


    @ApiOperation("上传图片")
    @PostMapping("uploadImage")
    public JsonResult uploadImage(@RequestParam("file") MultipartFile file,
                                   @ApiIgnore CurrentPrincipal currentPrincipal) throws IOException {
        String suffix = file.getOriginalFilename().substring(
                file.getOriginalFilename().lastIndexOf(".")
        );
        if (suffix.equalsIgnoreCase(".mp4")){
            return JsonResult.fail(ServiceCode.ERROR_UPLOAD_EMPTY,"上传文件格式错误");
        }
        final String newFileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"))+currentPrincipal.getId();

        final String uploadUrl = FileUploadUtil.uploadImage(file.getBytes(), newFileName + suffix);
        return JsonResult.ok(uploadUrl);
    }


    @ApiOperation("刪除图片")
    @PostMapping("deleteImage")
    public JsonResult deleteImage(String url){
        String fileName = url.substring(url.lastIndexOf("/") + 1);
        FileUploadUtil.deleteImage(fileName);
        return JsonResult.ok();
    }

//    @ApiOperation("刪除視頻")
//    @PostMapping("deleteVideo")
//    public JsonResult deleteVideo(String fileUrl){
//        String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
//        FileUploadUtil.deleteVideo(fileName);
//        return JsonResult.ok();
//    }
}